/*
 * LoadQueryPanel.java
 *
 * Created on 29-Sep-2013, 11:36:33 AM
 */
package ngram_reader.viewer;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import ngram.util.GuiUtil;
import ngram.util.IOUtil;
import ngram_reader.viewer.query.AllStatSearchQuery;
import ngram_reader.viewer.query.NGramSearchQuery;
import ngram_reader.viewer.query.NoStatSearchQuery;
import ngram_reader.viewer.query.NoStatSearchQuery.Match;
import ngram_reader.viewer.query.SearchQueryFactory;
import ngram_reader.viewer.query.StatisticsSearchQuery.Aggregate;
import processorIO.FileIO;
import swingExtended.todoQueue.TaskManager;
import swingExtended.SwingExtended;

/**
 *
 * @author Shimu
 */
public class LoadQueryPanel extends javax.swing.JPanel {

    private TaskManager<NGramSearchQuery> taskManager;

    /** Creates new form LoadQueryPanel */
    public LoadQueryPanel() {
        initComponents();
        GuiUtil.addEnumAsOptions(Match.class, this.dropDownMatch);
    }

    public void setTaskManager(TaskManager<NGramSearchQuery> manager) {
        this.taskManager = manager;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelDescription = new javax.swing.JLabel();
        inputBrowserPanel1 = new swingExtended.inputBrowsePanel.InputBrowserPanel();
        loadButton = new javax.swing.JButton();
        dropDownMatch = new javax.swing.JComboBox();
        checkBoxMatchCase = new javax.swing.JCheckBox();
        spinnerNGramStart = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        checkBoxIgnoreStatistics = new javax.swing.JCheckBox();

        labelDescription.setText("Load a newline separated file of queries");

        loadButton.setText("Load");
        loadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadButtonActionPerformed(evt);
            }
        });

        checkBoxMatchCase.setText("Match Case");
        checkBoxMatchCase.setToolTipText("The n-gram must match the case exactly.");

        spinnerNGramStart.setModel(new javax.swing.SpinnerNumberModel(1, 1, 5, 1));
        spinnerNGramStart.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinnerNGramStartStateChanged(evt);
            }
        });

        jLabel2.setText("gram");

        jLabel1.setText("Search");

        checkBoxIgnoreStatistics.setText("Ignore Statistics");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelDescription)
                    .addComponent(inputBrowserPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(spinnerNGramStart, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dropDownMatch, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkBoxMatchCase))
                    .addComponent(loadButton, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(checkBoxIgnoreStatistics))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelDescription)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(inputBrowserPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerNGramStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(dropDownMatch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkBoxMatchCase))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(checkBoxIgnoreStatistics)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 140, Short.MAX_VALUE)
                .addComponent(loadButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadButtonActionPerformed
        BufferedReader reader;
        List<String> content;
        try {
            reader = IOUtil.getBufferedReader(inputBrowserPanel1.getFile().getAbsolutePath());
            content = FileIO.getContent(reader);
        } catch (IOException ex) {
            SwingExtended.openWarningWindow(this, "Could not load file", ex);
            return;
        }

        String gram = spinnerNGramStart.getValue().toString();
        Match match = (Match) dropDownMatch.getSelectedItem();
        boolean ignoreStat = checkBoxIgnoreStatistics.isSelected();

        for (String line : content) {
            try {
                final NGramSearchQuery searchQuery;
                if (ignoreStat) {
                    searchQuery = SearchQueryFactory.getNoStatSearchQuery(line.trim(), match, gram, gram, checkBoxMatchCase.isSelected());

                } else {
                    searchQuery = SearchQueryFactory.getAllStatSearchQuery(line.trim(), match, gram, gram, checkBoxMatchCase.isSelected());
                }
                taskManager.addItem(searchQuery);
            } catch (Exception ex) {
                SwingExtended.openWarningWindow(this, "", ex);
                continue;
            }

        }
    }//GEN-LAST:event_loadButtonActionPerformed

    private void spinnerNGramStartStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerNGramStartStateChanged
        }//GEN-LAST:event_spinnerNGramStartStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox checkBoxIgnoreStatistics;
    private javax.swing.JCheckBox checkBoxMatchCase;
    private javax.swing.JComboBox dropDownMatch;
    private swingExtended.inputBrowsePanel.InputBrowserPanel inputBrowserPanel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel labelDescription;
    private javax.swing.JButton loadButton;
    private javax.swing.JSpinner spinnerNGramStart;
    // End of variables declaration//GEN-END:variables
}
